package demo1;

import java.util.Arrays;

public class SeqList {
    private int[] array ;
    private final int  DEFAULT_CAPACITY = 10 ;
    private int usedSize ;
    public SeqList(){
    }
    public SeqList(int capacity){
        this.array = new int[capacity];
    }
    private boolean isEmpty(){
        if(this.usedSize == 0){
            return true ;
        }
        return false ;
    }
    public void add(int data){
        //判断数组是否为空
        if(this.array == null){
            this.array = new int[DEFAULT_CAPACITY];
        }
         // 把元素添加到最后;
        this.array[this.usedSize ++ ] = data ;
        isFull();
    }
    private void isFull(){
        //判断是否满了
        if(this.usedSize == this.array.length){
            this.array = Arrays.copyOf(array , array.length * 2);
        }
    }
    public int size(){
        return usedSize ;
    }
    public void add(int pos , int data){
         checkPos(pos);
         if(pos == this.usedSize){
             add(data);
          return;
         }
        for (int i = usedSize; i > pos; i--) {
            array[i] = array[i-1];
        }
        array[pos] = data ;
        this.usedSize ++ ;
        isFull();
    }
    public int indexOf(int toFind){
        for(int i = 0 ; i < this.usedSize ; i ++){
            if(array[i] == toFind){
                return i ;
            }
        }
        return -1 ;

    }
    public void set(int pos , int data){
        if(pos < 0 || pos >= usedSize){
            throw new IndexOutException("pos:"+pos+" is illegality");
        }
        array[pos] = data;
    }

    public void remove(int val){
       int index =  indexOf(val);
       if(index == -1){
           return;
       }
        for (int i = index; i < usedSize-1; i++) {
            array[i] = array[i+1];
        }
        usedSize -- ;
    }
   public void clear(){
        this.usedSize = 0;
   }
    public boolean contains(int val){
        for(int i = 0 ; i < this.usedSize ; i ++){
            if(array[i] == val){
                return true ;
            }
        }
        return false ;
    }

    public int get(int pos){
        if(pos < 0 || pos >= usedSize){
            throw new IndexOutException("pos:"+pos+" is illegality");
        }
       return this.array[pos];
    }
    public void display(){
        for (int i = 0; i < usedSize; i++) {
            System.out.print(this.array[i] + " ");
        }
        System.out.println();
    }
    private boolean checkPos(int pos){
        if(pos < 0 || pos > usedSize){
            throw new IndexOutException("pos:"+pos+" is illegality");
        }
        return true ;
    }

}
